using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace tKitchen
{
    public class ShoppingListPresenter
    {
        tKitchenMain domain;
        ShoppingList inputForm;
        List<ShoppingItem> itemList;
        int maxId = 0;

        public ShoppingListPresenter(tKitchenMain domainIn)
        {
            InitForm(domainIn);
        }
        public ShoppingListPresenter(tKitchenMain domainIn, List<Recipe_Ingredient> listIn)
        {
            InitForm(domainIn);

            //convert the recipe ingredients to shopping items
            //and add them to the list
            foreach (Recipe_Ingredient ing in listIn)
            {
                itemList.Add(new ShoppingItem(GetNewId(), ing.Amount, ing.Unit.Name, ing.Ingredient.Name));
            }


            PopulateGridView();
        }

        private int GetNewId()
        {
            //return the current id
            int id = maxId;

            //increment the maxId
            maxId++;


            return id;
            
        }




        private void InitForm(tKitchenMain domainIn)
        {
            domain = domainIn;
            inputForm = new ShoppingList();
            inputForm.CheckAllChanged += new EventHandler(inputForm_CheckAllChanged);
            inputForm.AddButtonClicked += new EventHandler(inputForm_AddButtonClicked);
            inputForm.DeleteButtonClicked += new EventHandler(inputForm_DeleteButtonClicked);
            

            itemList = new List<ShoppingItem>();
        }





        #region Event Handlers
        void inputForm_AddButtonClicked(object sender, EventArgs e)
        {
            //get the values from the text boxes
            string name = inputForm.Item;
            double amount = Double.Parse(inputForm.Amount);
            string unit = inputForm.Unit;
            
            //add a new ShoppingItem to the list
            itemList.Add(new ShoppingItem(GetNewId(),amount,unit,name));

            //repopulate the display
            PopulateGridView();

            //clear the boxes
            inputForm.Item = "";
            inputForm.Amount = "";
            inputForm.Unit = "";

        }
        void inputForm_DeleteButtonClicked(object sender, EventArgs e)
        {
            /*used for deleting by checkboxes
            //for each row
            for (int i = 0; i < inputForm.ItemList.Rows.Count; i++)
            {
                //see if the box is checked
                DataGridViewCheckBoxCell tempCell = (DataGridViewCheckBoxCell)inputForm.ItemList.Rows[i].Cells["selected"];
                Boolean boxValue = (Boolean)tempCell.Value;
                if (boxValue.Equals(true))
                {
                    //delete that row
                    inputForm.ItemList.Rows.RemoveAt(i);
                }
            }
            */

            //determine which row is selected
            int rowIndex = inputForm.ItemList.SelectedCells[0].RowIndex;
            
            //get the id of the item in that row
            int itemId = (int)inputForm.ItemList.Rows[rowIndex].Cells["Id"].Value;
            //remove that item from the list
            ShoppingItem tempItem = FindListItem(itemId);
            itemList.Remove(tempItem);

            //repopulate the list
            PopulateGridView();

        }

       

        //not implemented yet
        void inputForm_CheckAllChanged(object sender, EventArgs e)
        {
            bool boxChecked = inputForm.CheckAll.Checked;
            for (int i = 0; i < inputForm.ItemList.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell tempCell = (DataGridViewCheckBoxCell)inputForm.ItemList.Rows[i].Cells["selected"];
                tempCell.Value = boxChecked;
            }

        }


        #endregion


        /// <summary>
        /// Returns the ShoppingItem in the list that has the specified
        /// Id
        /// </summary>
        /// <param name="itemId"></param>
        /// <returns></returns>
        private ShoppingItem FindListItem(int itemId)
        {
            ShoppingItem tempItem = null;
            int i = 0;
            while (tempItem == null && i < itemList.Count)
            {
                if (itemList[i].Id == itemId)
                {
                    tempItem = itemList[i];
                }
                i++;
            }
            return tempItem;
        }

        private void PopulateGridView()
        {
            inputForm.ItemList.DataSource = null;
            inputForm.ItemList.DataSource = itemList;
            inputForm.ItemList.Columns["Id"].Visible = false;
            /*
            DataGridViewColumn tempCol = inputForm.ItemList.Columns["Selected"];
            tempCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            tempCol.Width = 20;
            tempCol.HeaderText = "";

            //set the default value of the checkbox to false
            for (int i = 0; i < inputForm.ItemList.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell tempCell = (DataGridViewCheckBoxCell)inputForm.ItemList.Rows[i].Cells["Selected"];
                tempCell.Value = false;
            }
             */

        }

        internal void ShowForm()
        {
            inputForm.Show();
        }

    }
    public class ShoppingItem
    {
        int id;
        //bool selected;
        double amount;
        string unit;
        string name;
        public ShoppingItem(int idIn, double amountIn, string unitIn, string nameIn)
        {
            id = idIn;
            //selected = false;
            amount = amountIn;
            unit = unitIn;
            name = nameIn;
        }

        #region Properties
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        /* public bool Selected
         {
             get { return selected; }
             set { selected = value; }
         }
         */
        public double Amount
        {
            get { return amount; }
            set { amount = value; }
        }
        public string Unit
        {
            get { return unit; }
            set { unit = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        #endregion

        public override string ToString()
        {
            return amount.ToString() + " " + unit.ToString() + " " + name;
        }
    }
}
